﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter09.ViewModel
{
    using System.Windows;
    public class CommandHistoryItem
    {
        public string CommandName { get; set; }
        public UIElement ElementActedOn { get; set; }
        public string PropertyActedOn { get; set; }
        public object PreviousState { get; set; }

        public CommandHistoryItem(string commandName, UIElement elementActedOn, string propertyActedOn, object previousSatte)
        {
            CommandName = commandName;
            ElementActedOn = elementActedOn;
            PropertyActedOn = propertyActedOn;
            PreviousState = previousSatte;
        }

        public CommandHistoryItem(string commandName)
            : this(commandName, null, null, null)
        {

        }

        public bool CanUndo
        {
            get { return (ElementActedOn != null && PropertyActedOn != null); }
        }

        public void Undo()
        {
            var elementType = ElementActedOn.GetType();
            var property = elementType.GetProperty(PropertyActedOn);
            property.SetValue(ElementActedOn, PreviousState, null);
        }
    }
}
